commonlibsse_ng\re\m/
MemoryManager.rs

1pub mod SimpleArray;
2pub mod alloc;
3
4pub use self::alloc::tes_global::TESGlobalAlloc;
5
6use core::ffi::c_void;
7use core::ptr;
8
9use crate::re::BSSmallBlockAllocator::BSSmallBlockAllocator;
10use crate::re::CompactingStore;
11use crate::re::IMemoryHeap::IMemoryHeap;
12use crate::re::ScrapHeap::ScrapHeap;
13
14/// Represents a thread-local heap in the memory manager.
15#[repr(C)]
16pub struct ThreadScrapHeap {
17    pub heap: ScrapHeap,            // 0x00
18    pub next: *mut ThreadScrapHeap, // 0x90
19    pub owningThread: u32,          // 0x98
20    pub pad: u32,                   // 0x9C
21}
22const _: () = assert!(core::mem::size_of::<ThreadScrapHeap>() == 0xA0);
23
24/// Memory manager interface
25#[repr(C)]
26#[derive(Debug)]
27pub struct MemoryManager {
28    pub initialized: bool,                               // 0x000
29    pub numHeaps: u16,                                   // 0x002
30    pub numPhysicalHeaps: u16,                           // 0x004
31    pub heaps: *mut *mut IMemoryHeap,                    // 0x008
32    pub allowOtherContextAllocs: *mut bool,              // 0x010
33    pub heapsByContext: [*mut IMemoryHeap; 127],         // 0x018
34    pub threadScrapHeap: *mut ThreadScrapHeap,           // 0x410
35    pub physicalHeaps: *mut *mut IMemoryHeap,            // 0x418
36    pub bigAllocHeap: *mut IMemoryHeap,                  // 0x420
37    pub emergencyHeap: *mut IMemoryHeap,                 // 0x428
38    pub smallBlockAllocator: *mut BSSmallBlockAllocator, // 0x430
39    pub compactingStore: *mut CompactingStore::Store,    // 0x438
40    pub externalHavokAllocator: *mut IMemoryHeap,        // 0x440
41    pub specialHeaps: bool,                              // 0x448
42    pub allowPoolUse: bool,                              // 0x449
43    pub pad44A: [u8; 2],                                 // 0x44A
44    pub sysAllocBytes: u32,                              // 0x44C
45    pub mallocBytes: u32,                                // 0x450
46    pub alignmentForPools: u32,                          // 0x454
47    pub mainThreadMemoryProblemPassSignal: u32,          // 0x458
48    pub failedAllocationSize: usize,                     // 0x460
49    pub numMemoryProblemPassesRun: u32,                  // 0x468
50    pub timeOfLastMemoryProblemPass: usize,              // 0x470
51    pub defaultHeap: *mut IMemoryHeap,                   // 0x478
52}
53const _: () = assert!(core::mem::size_of::<MemoryManager>() == 0x480);
54
55impl Default for MemoryManager {
56    #[inline]
57    fn default() -> Self {
58        Self::new()
59    }
60}
61
62impl MemoryManager {
63    #[inline]
64    pub const fn new() -> Self {
65        Self {
66            initialized: false,
67            numHeaps: 0,
68            numPhysicalHeaps: 0,
69            heaps: ptr::null_mut(),
70            allowOtherContextAllocs: ptr::null_mut(),
71            heapsByContext: [ptr::null_mut(); 127],
72            threadScrapHeap: ptr::null_mut(),
73            physicalHeaps: ptr::null_mut(),
74            bigAllocHeap: ptr::null_mut(),
75            emergencyHeap: ptr::null_mut(),
76            smallBlockAllocator: ptr::null_mut(),
77            compactingStore: ptr::null_mut(),
78            externalHavokAllocator: ptr::null_mut(),
79            specialHeaps: false,
80            allowPoolUse: true,
81            pad44A: [0; 2],
82            sysAllocBytes: 0,
83            mallocBytes: 0,
84            alignmentForPools: 0,
85            mainThreadMemoryProblemPassSignal: 0,
86            failedAllocationSize: 0,
87            numMemoryProblemPassesRun: 0,
88            timeOfLastMemoryProblemPass: 0,
89            defaultHeap: ptr::null_mut(),
90        }
91    }
92
93    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 11045, ae_id = 11141)]
94    pub unsafe fn GetSingleton() -> *mut MemoryManager {}
95
96    /// Allocate memory.
97    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 66859, ae_id = 68115)]
98    pub unsafe fn Allocate(
99        &mut self,
100        size: usize,
101        alignment: i32,
102        alignment_required: bool,
103    ) -> *mut c_void {
104    }
105
106    /// Deallocate memory.
107    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 66861, ae_id = 68117)]
108    pub unsafe fn Deallocate(&mut self, mem: *mut c_void, alignment_required: bool) {}
109
110    /// Get thread-local scrap heap.
111    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 66841, ae_id = 68088)]
112    pub unsafe fn GetThreadScrapHeap(&mut self) -> *mut ScrapHeap {}
113
114    /// Reallocate memory.
115    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 66860, ae_id = 68116)]
116    pub unsafe fn Reallocate(
117        &mut self,
118        old_mem: *mut c_void,
119        new_size: usize,
120        alignment: i32,
121        aligned: bool,
122    ) -> *mut c_void {
123    }
124
125    /// Register the memory manager.
126    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 35199, ae_id = 36091)]
127    pub unsafe fn RegisterMemoryManager(&mut self) {}
128}
129
130/// # Safety
131#[inline]
132pub unsafe fn malloc(size: usize) -> *mut c_void {
133    unsafe { MemoryManager::GetSingleton().as_mut() }
134        .map_or(ptr::null_mut(), |heap| unsafe { heap.Allocate(size, 0, false) })
135}
136
137/// # Safety
138#[inline]
139pub unsafe fn free(ptr: *mut c_void) {
140    unsafe {
141        if let Some(heap) = MemoryManager::GetSingleton().as_mut() {
142            heap.Deallocate(ptr, false);
143        };
144    }
145}